<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('test', function () {
    $ps_1 = \App\Models\Province::where('level',1)->get();
    foreach ($ps_1 as $ps) {
        foreach ($ps->children as $pc) {
            if($pc->level!=($ps->level+1)){
                $pc->level+=1;
                $pc->save();
            }
            foreach ($pc->children as $pc1) {
                if($pc1->level!=($pc->level+1)){
                    $pc1->level+=1;
                    $pc1->save();
                }
            }
        }
    }
    die(1);
    $user = \App\Models\Admin::find(3);
    dd($user->shop);
    $res = responseZK(1,['status'=>1]);
    dd($res->getData()->status);
    return responseZK(1,['status'=>1]);
//    dd(random_int(1,5));
//    $statistics_repostory = app()->build(\App\Repositories\StatisticsRepository::class);
//    $distribution_statistics_repostory = app()->build(\App\Repositories\DistributionStatisticsRepository::class);
//    $statistics_repostory->generate(30,3);
//    //配送月统计
//    $users = \App\User::all();
//    foreach ($users as $user) {
//        if($user->is_distribution_statistics)
//            $distribution_statistics_repostory->generate($user,1,3);
//    }
//    dd("统计");
//    $distribution_statistics_repostory = app()->build(\App\Repositories\DistributionStatisticsRepository::class);
//    //配送月统计
//    $users = \App\User::all();
//    foreach ($users as $user) {
//        if($user->is_distribution_statistics)
//            $distribution_statistics_repostory->generate($user,1,3,"2017-12-31");
//    }
//    die;
//    $app = new \EasyWeChat\Foundation\Application(config('easywechat'));
//    $payment = $app->payment;
//    $res = $payment->queryRefund('2017121708335957');
////    $res = $payment->refund("2017121708335957",
////        "2017121708335999", 180.84*100,180.84*100);
//    dd($res);
    $s = \App\Models\Admin::find(3);
    $s->password = bcrypt('first');
    $s->save();
    die;
    $category_statistics_repostory = app()->build(\App\Repositories\CategoryStatisticsRepository::class);
    $categories = \App\Models\Category::all();
    $date = request('date',date('Y-m-d'));
    $time = strtotime($date);
    $date = date('Y-m-d',$time);
    while ($date!="2017-11-15"){
        foreach ($categories as $category) {
            $category_statistics_repostory->generate($category->id,1,1,$date);
        }
        $time -= 3600*24;
        $date = date('Y-m-d',$time);
    }
    die;
    $distribution_statistics_repostory = app()->build(\App\Repositories\DistributionStatisticsRepository::class);
    $users = \App\User::all();
    foreach ($users as $user) {
        if($user->is_distribution_statistics)
            $distribution_statistics_repostory->generate($user);
    }
    die;
    $order_Res = app()->build(\App\Repositories\OrderRepository::class);
    $order_Res->mergeOrder(\App\Models\Order::find(12345));
    die;
    $distribution_statistics_repostory = app()->build(\App\Repositories\DistributionStatisticsRepository::class);
    $users = \App\User::all();
    foreach ($users as $user) {
        if($user->is_distribution_statistics)
            $distribution_statistics_repostory->generate($user,1,1,'2017-11-27');
    }
    die;
    $response = \App\ZL\ORG\Aliydx\Sms::sendSms(
        "你好菜果蔬生鲜", // 短信签名
        "SMS_113620021", // 短信模板编号
        "13867558443", // 短信接收者
        Array(  // 短信模板中字段的值
            "code"=>"4222",
        ),
        "123"   // 流水号,选填
    );
    dd($response);
    die;
    $t = sprintf("%.0f",(33.80-0)*100);
    dd($t);
    $category_statistics_repostory = app()->build(\App\Repositories\CategoryStatisticsRepository::class);
    $categories = \App\Models\Category::all();
    foreach ($categories as $category) {
        $category_statistics_repostory->generate($category->id);
    }
    die;
    $user = \App\User::find(1);
    dd($user->checkAbility('pairing'));
//    $orders = \Illuminate\Support\Facades\DB::table('goods')->where([
//        ['deleted_at','>',1],
//    ])->update(['status'=>2,'deleted_at'=>null]);
//    dump($orders);
//    foreach ($orders as $order) {
//        $order->status=2;
//        $order->save();
//    }
//    $orders = \App\Models\Order::whereNotNull('shipping_at')->update(['shipping_status'=>1]);->where('created_at','>',date('Y-m-d'))
//删除购物车错误数据
//    $carts = \App\Models\Cart::whereNull('attribute_id')->whereNotNull('goods_id')->get();
//    foreach ($carts as $cart) {
//        if($cart->goods->attributesByStatus->count()>0){
//            $cart->delete();
//        }
//    }

//    $st = app()->build(\App\Repositories\StatisticsRepository::class);
//    $st->generate();
//    $users = \App\User::all();
//    foreach ($users as $v) {
//        if($v->xcx_openid || $v->xcx_openid==$v->openid){
//            $v->openid = null;
//            $v->save();
//        }
//    }
//    \App\Models\Order::where('shipping_status', 0)->update(['is_ship'=>0]);
    //改密码
    $ids = \App\Models\Role::find(2)->users;
    dd($ids);
    dd(\App\User::where('xcx_openid',null)->first());
    return;
    //用户测试
    $user = \App\User::where('xcx_openid',null)->get();
    $user1 = \App\User::where('openid',null)->get();
    dd($user1->toArray());
    $arr = [];
    foreach ($user1 as $v) {
        $arr[$v->nickname] = $v->id;
    }
    dump($arr);
    foreach ($user as $v) {
//        $_us = \App\User::where('nickname',$v->nickname)->where('openid',null)->first();
        if(isset($arr[$v->nickname]))
            dd($arr[$v->nickname]);
    }
    dd($user);
    dd(request()->all());
    $users = \App\User::where('parent_id','>',0)->get();
    $users->load('bonuses');
    $name = '新用户满减券';
    foreach ($users as $user) {
        if($user->bonuses->count()==0){
//            dd($user);
            $bonus = \App\Models\Bonus::where([
                ['name',$name],
                ['user_id',0],
            ])->first();
            if($bonus){
                $bonus->user_id = $user->id;
                $bonus->save();
            }
            $bonus2 = \App\Models\Bonus::where([
                ['name',$name],
                ['user_id',0],
            ])->first();
            if($bonus2){
                $bonus2->user_id = $user->parent_id;
                $bonus2->save();
            }
        }
    }
});

Route::get('generate', function () {
//    $configs = \App\Models\Config::all();
//    foreach ($configs as $config) {
//        echo '[';
//        echo '\''.$config->group.'\',';
//        echo '\''.$config->type.'\',';
//        echo '\''.$config->title.'\'';
//        echo '],';
//        echo '</br>';
//    }
    $name = request('name');
    if(!$name)
        dd('no name');
    $password = bcrypt($name);
    $email = $name.'@'.$name.'.com';
    $admin = \App\Models\Admin::create([
        'name'=>$name,
        'email'=>$email,
        'password'=>$password,
    ]);
    $last_shop = \App\Models\Shop::orderBy('id','desc')->first();
    $shop = \App\Models\Shop::create([
        'admin_id'=>$admin->id,
        'name'=>$name,
    ]);
    $configs = [
        ['system','express_need_money','柯桥满30元免费配送'],
        ['system','share_title','分享标题'],
        ['system','skip_xcx_url','跳转小程序的域名'],
        ['hotKeyword','hotKeyword','热门词'],
        ['system','deliver_goods_interval_time','配送间隔时间'],
        ['shipping','shipping_area','配送区域'],
        ['system','navigates_background_image','导航条背景图(750x164)'],
        ['layout','index_layout','首页布局设置'],
        ['advertising','index_advertising','首页广告'],
        ['system','last_today','今日最迟'],
        ['system','last_tomorrow','明日最迟'],
        ['system','delivery_fee','配送费'],
        ['system','first_today','今日最早'],
        ['system','first_tomorrow','明日最早'],
        ['system','can_selftake','能否自提'],
        ['goods','attribute_types','规格类型'],
    ];
    foreach ($configs as $config) {
        \App\Models\Config::create([
            'shop_id'=>$shop->id,
            'group'=>$config[0],
            'type'=>$config[1],
            'value'=>$config[2],
        ]);
    }
    \App\Models\Wechat::create([
        'type'=>1,
        'admin_id'=>$admin->id,
        'shop_id'=>$shop->id,
    ]);
    $admin->shop_id = $shop->id;
    $admin->save();
    dd(1);
});
Route::get('generateP', function () {
    $json = require(dirname(__FILE__).'/../public/ssq.php');
    $data = json_decode($json,true);
    $array = [];
    foreach ($data as $v1) {
        if($v1['diji']=='' && $v1['xianji']==''){
            $array[$v1['ShengJiName']] = [
              'shenji'=>$v1['ShengJiName'],
              'text'=>$v1['ShengJiName'],
              'diji'=>$v1['diji'],
              'xianji'=>$v1['xianji'],
              'zhudi'=>$v1['ZhuDi'],
              'renkou'=>$v1['RenKou'],
              'mianji'=>$v1['MianJi'],
              'youbian'=>$v1['YouBian'],
              'quhao'=>$v1['quhao'],
              'quhua'=>$v1['quHuaDaiMa'],
              'level'=>1,
              'pid'=>0,
            ];
            $_p = \App\Models\Province::create($array[$v1['ShengJiName']]);
            $array[$v1['ShengJiName']]['id'] = $_p->id;
        }
        if($v1['diji']!='' && $v1['xianji']==''){
            $array[$v1['ShengJiName']][$v1['diji']] = [
              'shenji'=>$v1['ShengJiName'],
              'diji'=>$v1['diji'],
              'text'=>$v1['diji'],
              'xianji'=>$v1['xianji'],
              'zhudi'=>$v1['ZhuDi'],
              'renkou'=>$v1['RenKou'],
              'mianji'=>$v1['MianJi'],
              'youbian'=>$v1['YouBian'],
              'quhao'=>$v1['quhao'],
              'quhua'=>$v1['quHuaDaiMa'],
              'level'=>2,
              'pid'=>$array[$v1['ShengJiName']]['id'],
            ];
            $_p = \App\Models\Province::create($array[$v1['ShengJiName']][$v1['diji']]);
            $array[$v1['ShengJiName']][$v1['diji']]['id'] = $_p->id;
        }
        if($v1['diji']!='' && $v1['xianji']!=''){
            if(!isset($array[$v1['ShengJiName']][$v1['diji']])){
                $array[$v1['ShengJiName']][$v1['diji']] = [
                    'shenji'=>$v1['ShengJiName'],
                    'diji'=>$v1['diji'],
                    'text'=>$v1['diji'],
//                    'xianji'=>$v1['xianji'],
//                    'zhudi'=>$v1['ZhuDi'],
//                    'renkou'=>$v1['RenKou'],
//                    'mianji'=>$v1['MianJi'],
//                    'youbian'=>$v1['YouBian'],
//                    'quhao'=>$v1['quhao'],
//                    'quhua'=>$v1['quHuaDaiMa'],
                    'level'=>2,
                    'pid'=>$array[$v1['ShengJiName']]['id'],
                ];

                $_p = \App\Models\Province::create($array[$v1['ShengJiName']][$v1['diji']]);
                $array[$v1['ShengJiName']][$v1['diji']]['id'] = $_p->id;
            }
            $array[$v1['ShengJiName']][$v1['diji']][$v1['xianji']] = [
              'shenji'=>$v1['ShengJiName'],
              'diji'=>$v1['diji'],
              'xianji'=>$v1['xianji'],
              'text'=>$v1['xianji'],
              'zhudi'=>$v1['ZhuDi'],
              'renkou'=>$v1['RenKou'],
              'mianji'=>$v1['MianJi'],
              'youbian'=>$v1['YouBian'],
              'quhao'=>$v1['quhao'],
              'quhua'=>$v1['quHuaDaiMa'],
              'level'=>3,
              'pid'=>$array[$v1['ShengJiName']][$v1['diji']]['id'],
            ];
            \App\Models\Province::create($array[$v1['ShengJiName']][$v1['diji']][$v1['xianji']]);
        }
    }
});

Auth::routes();

Route::get('/', function () {
    return redirect()->action('Admin\HomeController@index');
});


Route::get('/share', 'Admin\HomeController@share');
Route::get('/index_advertising', 'Admin\HomeController@indexAdvertising');
Route::get('/tj', 'Admin\HomeController@tj');
Route::get('/mini_hint', 'Admin\HomeController@miniHint');

Route::any('temp/order/{id}', 'Admin\OrderController@orderInfo')->name('order.info');
Route::any('goods_panel', 'Admin\GoodsController@panel')->name('goods.panel');

Route::any('orders_panel', 'Admin\OrderController@panel')->name('orders.panel');
Route::any('orders_fresh', 'Admin\OrderController@fresh')->name('orders.fresh');

//公共号登录
Route::any('/wechat/login', 'Home\UserCenterController@goAuthorize');
Route::get('/wechat/callback', 'Home\UserCenterController@callback')->name('wechat.callback');

Route::group(['middleware' => 'home.checkwechatlogin','namespace' => 'Home'], function () {});

Route::group(['middleware' => 'auth.admin:admin','namespace' => 'Admin','prefix' => 'admin'], function () {

    Route::get('/', 'HomeController@index')->name('home');
    Route::any('kefu_page', 'HomeController@kefuPage')->name('kefu.page');

    Route::get('layouts/setting', 'LayoutController@setting')->name('layouts.setting');

    Route::any('advertising/setting', 'ConfigController@advertisingSetting')->name('configs.advertisingSetting');
    Route::any('advertising/store', 'ConfigController@advertisingSettingStore')->name('configs.advertisingSettingStore');

    //用户管理
    Route::get('/users/add_bonus', 'UserController@addBonus')->name('users.addBonus');
    Route::get('/users/add_role', 'UserController@addRole')->name('users.addRole');
    Route::get('/users/del_role', 'UserController@delRole')->name('users.delRole');
    Route::get('/users', 'UserController@index')->name('users.index');
    Route::get('/users/list', 'UserController@manageList')->name('users.list');
    Route::any('/users/setting', 'UserController@setting')->name('users.setting');
    Route::get('/users/setrole', 'UserController@setRole')->name('users.setRole');
    Route::get('/users/edit/{id}', 'UserController@edit')->name('users.edit');
    Route::patch('/users/{id}', 'UserController@update')->name('users.update');
    Route::get('/users/{id}', 'UserController@show')->name('users.show');
    Route::any('junior_del', 'UserController@juniorDel')->name('junior.del');

    //商品批量修改
    Route::any('/batch_operation', 'GoodsController@batchOperation')->name('goods.batch_operation');
    Route::any('/batch_operation_post', 'GoodsController@batchOperationPost')->name('goods.batch_operation_post');

    //配送单 页面
    Route::any('orders/peisongdan/{id}', 'OrderController@peisongdan')->name('orders.peisongdan');
    Route::any('orders/peisongdan/{id}', 'OrderController@peisongdan')->name('orders.peisongdan');
    Route::any('orders/shouhuo/{id}', 'OrderController@shouhuo')->name('orders.shouhuo');
    Route::any('orders/fahuozhong/{id}', 'OrderController@fahuozhong')->name('orders.fahuozhong');

    Route::any('orders/returnOrder', 'OrderController@returnOrder')->name('orders.returnOrder');
    Route::any('orders/returnGoods', 'OrderController@returnGoods')->name('orders.returnGoods');
    //退货
    Route::any('returnLogs/ok/{id}', 'OrderController@returnOk')->name('returnLogs.ok');
    Route::any('goods/del/{id}', 'GoodsController@del')->name('goods.del');
    Route::any('returnLogs/bar/{id}', 'OrderController@returnBar')->name('returnLogs.bar');

    Route::any('bonuses/statistics', 'BonusController@statistics')->name('bonuses.statistics');
    Route::any('bonuses/del_by_name', 'BonusController@delByName')->name('bonuses.delByName');

    Route::any('goodsEvaluates/reply', 'GoodsEvaluateController@reply')->name('goodsEvaluates.reply');
    Route::any('statistics/refresh/{id}', 'StatisticsController@refresh')->name('statistics.refresh');
    Route::any('categoryStatistics/refresh/{id}', 'CategoryStatisticsController@refresh')->name('categoryStatistics.refresh');

    Route::get('/configs/wechat', 'ConfigController@wechat')->name('configs.wechat');
    Route::get('/setting', 'ConfigController@setting')->name('configs.setting');

    Route::resource('categories', 'CategoryController');
    Route::resource('configs', 'ConfigController');
    Route::resource('goods', 'GoodsController');
    Route::resource('slideshows', 'SlideshowController');
    Route::resource('layouts', 'LayoutController');
    Route::resource('bonuses', 'BonusController');
    Route::resource('orders', 'OrderController');
    Route::resource('integralLogs', 'IntegralLogController');
    Route::resource('returnLogs', 'ReturnLogController');
    Route::resource('statistics', 'StatisticsController');
    Route::resource('categoryStatistics', 'CategoryStatisticsController');
    Route::resource('assets','AssetsController');
    Route::resource('orderBook','OrderBookController');
    Route::resource('roles', 'RoleController');
    Route::resource('abilities', 'AbilityController');
    Route::resource('goodsEvaluates', 'GoodsEvaluateController');
    Route::resource('navigates', 'NavigateController');
    Route::resource('distributionStatistics', 'DistributionStatisticsController');
    Route::resource('freights', 'FreightController');
//    Route::resource('goodsContents', 'GoodsContentController');
//    Route::resource('goodsAttributes', 'GoodsAttributeController');
//    Route::resource('goodsSlideshows', 'GoodsSlideshowController');

    //文章管理路由
    Route::get('/getArticle', 'ArticlesController@getArticle')->name('article.get');
    Route::post('/articleupload', 'ArticlesController@upload')->name('article.upload');
    Route::resource('article', 'ArticlesController');

});



Route::group(['middleware' => 'auth.admin:admin','namespace' => 'API','prefix' => 'api'], function () {

    Route::any('admin_configs_by_group/{group}', 'Admin\ConfigAPIAdminController@getConfigsByGroup');
    Route::any('setting_by_type', 'Admin\ConfigAPIAdminController@settingByType');
    Route::post('upload_admin', 'PicturesController@uploadAdmin');
    Route::post('admin_bonus_categories/distribute', 'Admin\BonusCategoryAPIAdminController@distribute');

    ## 微信设置
    Route::get('wechat_config', 'Admin\WechatAPIAdminController@getConfig');
    Route::get('wechat_setting', 'Admin\WechatAPIAdminController@setConfig');
    Route::post('index_layout_setting', 'Admin\ConfigAPIAdminController@setIndexLayout');
//    Route::get('/', 'HomeController@index')->name('home');
//    Route::any('kefu_page', 'HomeController@kefuPage')->name('kefu.page');
//
//    Route::get('layouts/setting', 'LayoutController@setting')->name('layouts.setting');
//
//    Route::any('advertising/setting', 'ConfigController@advertisingSetting')->name('configs.advertisingSetting');
//    Route::any('advertising/store', 'ConfigController@advertisingSettingStore')->name('configs.advertisingSettingStore');
//
//    //用户管理
//    Route::get('/users/add_bonus', 'UserController@addBonus')->name('users.addBonus');
//    Route::get('/users/add_role', 'UserController@addRole')->name('users.addRole');
//    Route::get('/users/del_role', 'UserController@delRole')->name('users.delRole');
//    Route::get('navigates', 'NavigateAPIController@index');
//    Route::get('/users/list', 'UserController@manageList')->name('users.list');
//    Route::any('/users/setting', 'UserController@setting')->name('users.setting');
//    Route::get('/users/setrole', 'UserController@setRole')->name('users.setRole');
//    Route::get('/users/edit/{id}', 'UserController@edit')->name('users.edit');
//    Route::patch('/users/{id}', 'UserController@update')->name('users.update');
//    Route::get('/users/{id}', 'UserController@show')->name('users.show');
//    Route::any('junior_del', 'UserController@juniorDel')->name('junior.del');
//
//    //商品批量修改
//    Route::any('/batch_operation', 'GoodsController@batchOperation')->name('goods.batch_operation');
//    Route::any('/batch_operation_post', 'GoodsController@batchOperationPost')->name('goods.batch_operation_post');
//
//    //配送单 页面
//    Route::any('orders/peisongdan/{id}', 'OrderController@peisongdan')->name('orders.peisongdan');
//    Route::any('orders/peisongdan/{id}', 'OrderController@peisongdan')->name('orders.peisongdan');
//    Route::any('orders/shouhuo/{id}', 'OrderController@shouhuo')->name('orders.shouhuo');
//    Route::any('orders/fahuozhong/{id}', 'OrderController@fahuozhong')->name('orders.fahuozhong');
//
//    Route::any('orders/returnOrder', 'OrderController@returnOrder')->name('orders.returnOrder');
//    Route::any('orders/returnGoods', 'OrderController@returnGoods')->name('orders.returnGoods');
//    //退货
//    Route::any('returnLogs/ok/{id}', 'OrderController@returnOk')->name('returnLogs.ok');
//    Route::any('goods/del/{id}', 'GoodsController@del')->name('goods.del');
//    Route::any('returnLogs/bar/{id}', 'OrderController@returnBar')->name('returnLogs.bar');
//
//    Route::any('bonuses/statistics', 'BonusController@statistics')->name('bonuses.statistics');
//    Route::any('bonuses/del_by_name', 'BonusController@delByName')->name('bonuses.delByName');
//
//    Route::any('goodsEvaluates/reply', 'GoodsEvaluateController@reply')->name('goodsEvaluates.reply');
//    Route::any('statistics/refresh/{id}', 'StatisticsController@refresh')->name('statistics.refresh');
//    Route::any('categoryStatistics/refresh/{id}', 'CategoryStatisticsController@refresh')->name('categoryStatistics.refresh');
//
//    Route::get('/configs/wechat', 'ConfigController@wechat')->name('configs.wechat');
    //商品
    Route::resource('admin_attribute_type', 'Admin\AttributeTypeAPIAdminController');

    Route::resource('admin_attribute_type', 'Admin\AttributeTypeAPIAdminController');
    Route::resource('admin_freights', 'Admin\FreightAPIAdminController');
    Route::resource('admin_categories', 'Admin\CategoryAPIAdminController');
    Route::resource('admin_bonus_categories', 'Admin\BonusCategoryAPIAdminController');
    Route::resource('admin_navigates', 'Admin\NavigateAPIAdminController');
    Route::resource('admin_articles', 'Admin\ArticlesAPIAdminController');
    Route::resource('admin_configs', 'Admin\ConfigAPIAdminController');
//    Route::resource('configs', 'ConfigController');
    Route::resource('admin_goods', 'Admin\GoodsAPIAdminController');
    Route::resource('admin_users', 'Admin\UsersAPIAdminController');
    Route::resource('admin_integral_logs', 'Admin\IntegralLogAPIAdminController');
    Route::resource('admin_goods_evaluates', 'Admin\GoodsEvaluateAPIAdminController');
//    Route::resource('slideshows', 'SlideshowController');
//    Route::resource('layouts', 'LayoutController');
//    Route::resource('bonuses', 'BonusController');
//    Route::resource('orders', 'OrderController');
//    Route::resource('integralLogs', 'IntegralLogController');
//    Route::resource('returnLogs', 'ReturnLogController');
//    Route::resource('statistics', 'StatisticsController');
//    Route::resource('categoryStatistics', 'CategoryStatisticsController');
//    Route::resource('roles', 'RoleController');
//    Route::resource('abilities', 'AbilityController');
//    Route::resource('goodsEvaluates', 'GoodsEvaluateController');
//    Route::resource('distributionStatistics', 'DistributionStatisticsController');
//    Route::resource('goodsContents', 'GoodsContentController');
//    Route::resource('goodsAttributes', 'GoodsAttributeController');
//    Route::resource('goodsSlideshows', 'GoodsSlideshowController');

    //文章管理路由
//    Route::get('/getArticle', 'ArticlesController@getArticle')->name('article.get');
//    Route::post('/articleupload', 'ArticlesController@upload')->name('article.upload');
//    Route::resource('article', 'ArticlesController');

});

//Route::get('/home', 'Admin\HomeController@index')->name('home');







//Route::resource('carts', 'CartController');
//
//Route::resource('orderGoods', 'OrderGoodsController');
//
//Route::resource('addresses', 'AddressController');
//
//Route::resource('payRecords', 'PayRecordController');




//Route::resource('goodsNumberLogs', 'GoodsNumberLogController');


//Route::resource('goodsEvaluates', 'GoodsEvaluateController');







//Route::resource('freightAreas', 'FreightAreaController');